% Copyright (C) 2014-19 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

%% Notes:
% Requires running table_2.m with 
%   unconditional_model_dummy = 1;
%   sign_dummy = 0;
% for the three options 
%   impulse_name = 'g_growth_fe'; % OE shocks
%   impulse_name = 'OECD_Forecast_Error_g'; % OECD shocks
%   impulse_name = 'BP_shock_base_spread_control'; % VAR shocks (max sample)
% to generate the
% mat-files loaded in this file

fid_g=fopen(['Results/Descriptives_table_FC_models.txt'],'w+');
topstring_descriptives={'\begin{table}[t!]'
'  \centering'
'  \caption{Sample range for the three different forecasting models}'
'   \tabcolsep=.2in'
'  \scalebox{0.9}{'
'    \begin{tabular}{lcccccc}'
'    \toprule'
'    & \multicolumn{2}{c}{OE} & \multicolumn{2}{c}{OECD} & \multicolumn{2}{c}{VAR}\\'
'    \midrule'
'    Country   & Range & T & Range & T & Range & T \\'
'    \midrule'};
for ii=1:size(topstring_descriptives,1)
fprintf(fid_g,'%-150s \n',topstring_descriptives{ii,1});
end

Data.OECD=load('Results/Descriptives_OECD_Forecast_Error_g');
Data.OECD.country_info_cell(:,2)=strrep(Data.OECD.country_info_cell(:,2),'Q1','S1');
Data.OECD.country_info_cell(:,2)=strrep(Data.OECD.country_info_cell(:,2),'Q3','S2');
Data.OECD.country_info_cell(:,3)=strrep(Data.OECD.country_info_cell(:,3),'Q1','S1');
Data.OECD.country_info_cell(:,3)=strrep(Data.OECD.country_info_cell(:,3),'Q3','S2');
Data.OE=load('Results/Descriptives_g_growth_fe');
Data.BP=load('Results/Descriptives_BP_shock_base_spread_control');
first_obs_new=cell2mat(Data.BP.country_info_cell(:,6))-1.25;
for country_iter=1:size(first_obs_new,1)
    Data.BP.country_info_cell{country_iter,2}=[num2str(floor(first_obs_new(country_iter))),'Q',num2str(mod(first_obs_new(country_iter),1)/0.25+1)];
end
identification_strings={'OE';'OECD';'BP'};
country_names=union(Data.OECD.country_info_cell(:,1),union(Data.OE.country_info_cell(:,1),Data.BP.country_info_cell(:,1)));

sum_obs=zeros(size(identification_strings,1),1);
for country_iter=1:size(country_names,1)
    country_info{country_iter,1}=country_names{country_iter};
    for identification_iter=1:size(identification_strings,1)
        country_pos=strmatch(country_names{country_iter},Data.(identification_strings{identification_iter}).country_info_cell(:,1),'exact');
        if ~isempty(country_pos)
            country_info{country_iter,1+2*(identification_iter-1)+1}=[Data.(identification_strings{identification_iter}).country_info_cell{country_pos,2}(3:end) '-' Data.(identification_strings{identification_iter}).country_info_cell{country_pos,3}(3:end)];
            country_info{country_iter,1+2*(identification_iter-1)+2}=num2str(Data.(identification_strings{identification_iter}).country_info_cell{country_pos,4});
            sum_obs(identification_iter,1)=sum_obs(identification_iter,1)+Data.(identification_strings{identification_iter}).country_info_cell{country_pos,4};
        else
            country_info{country_iter,1+2*(identification_iter-1)+1}='-';
            country_info{country_iter,1+2*(identification_iter-1)+2}='-';
        end
    end
        fprintf(fid_g,'%-30s \t & \t %15s \t & \t %6s \t & \t %15s \t & \t %6s \t & \t %15s \t & \t %6s \\\\ \n',...
    country_info{country_iter,:});
end
fprintf(fid_g,'%-30s \t & \t %15s \t & \t %6s \t & \t %15s \t & \t %6s \t & \t %15s \t & \t %6s \\\\ \n',...
    'Total','',num2str(sum_obs(1)),'',num2str(sum_obs(2)),'',num2str(sum_obs(3)));



bottomstring_descriptives={'    \bottomrule'
'    \end{tabular}'
' }'
'\caption*{\footnotesize{\emph{Notes:} Range refers to the first and last observation available. Note that the \textcite{BlanchardPerotti02}-approach requires 5 observations to construct 4 lags of growth rates.'
'   $T$ refers to the number of observations used for the particular country after accounting for missing values and lag construction in the unconditional model.}}'
'  \label{tab:descriptives_FC}'
'\end{table}'};

for ii=1:size(bottomstring_descriptives,1)
fprintf(fid_g,'%-150s \n',bottomstring_descriptives{ii,1});
end

fclose(fid_g)
